Page({
  data: {
    words:getApp().globalData.words,
    currentIndex: 0,
    currentWord: {e:'',c:'',s:''},
    favorites:{},
    letters:['A','B','C','D'],
    isImageChange:false,
    imageSrc1:'/pages/img/887cdff6f914426f92d5d49407414eb2.png',
    imageSrc2:'/pages/img/00788e60b9304b8bb173db45508c2db7.png'
  },
  nextWord:function(){
    let newIndex=(this.data.currentIndex+1)%this.data.words.length;
    this.setData({
      currentIndex:newIndex,
      currentWord:this.data.words[newIndex]
    });
  },
  onLoad: function() {
    this.setData({
      currentWord: this.data.words[0],
      options: this.generateOptions1(0)
    });
  },
  waiting:function(){
    wx.showToast({
      title: '查看详情',
      icon:'loading',
      duration:500
    })
  },
  generateOptions1:function(index){
    const correct=this.data.words[index];
    const option=[correct];
    const usedI=new Set([index]);
    for(let i=0;i<3;i++){
      let randomIndex;
      do{
        randomIndex=Math.floor(Math.random()*this.data.words.length);
      }while(usedI.has(randomIndex));
      option.push(this.data.words[randomIndex]);
      usedI.add(randomIndex);
    }
    return option.sort(()=>(Math.random()-0.5));
  },
  changePage:function(){
    setTimeout(()=>{
      this.nextWord();
      this.setData({
        selectedIndex:null,
        options:this.generateOptions1(this.data.currentIndex),
        isImageChange:false
      });
    },500);
    wx.showToast({ 
      title:'下一个',
      icon:'loading',
      duration:500
    });
  },
  selectOption1:function(e){
    const selectedIndex=e.currentTarget.dataset.index;
    const isCorrect=this.data.options[selectedIndex].c===this.data.currentWord.c;
    this.setData({
      selectedIndex,
      isCorrect
    });
    if(isCorrect){
      setTimeout(()=>{
        this.nextWord();
        this.setData({
          selectedIndex:null,
          options:this.generateOptions1(this.data.currentIndex),
          isImageChange:false
        });
      },1000);
      wx.showToast({ 
        title:'答案正确',
        icon:'success',
        duration:500
      });
    }else{
      wx.showToast({
        title:'答案错误',
        icon:'error'
      });
    }
  },
  addFavorite:function(){
    const word=this.data.currentWord;
    const wordKey=word.e;
    let favorites=this.data.favorites;
    this.setData({isImageChange:true})
    if(favorites[wordKey]){
      wx.showToast({
        title: '已收藏过了',
        icon:'success',
        duration:500
      });
    }else{
      favorites[wordKey]=word;
      wx.showToast({
        title: '收藏成功',
        icon:'success'
      });
      console.log(favorites);
    }
    this.setData({
      favorites,
    });
    const app = getApp();
    app.globalData.favorites = favorites;
    wx.setStorageSync('favorites', favorites);
    const mainPage = getCurrentPages().find(page => page.route === 'pages/main1/main1')
    if(mainPage){
      mainPage.setData({
        reviewlen:Object.keys(app.globalData.favorites).length
      })
    }
  },
})